home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.pl1,comp.lang.c
- Subject: Re: PL/I and C
- Date: 26 Feb 1996 13:07:17 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4gt0d5$7v9@solutions.solon.com>
- References: <4gh5ru$eng@goanna.cs.rmit.EDU.AU> <AD536AAB9668B76CD@mcdialb09.it.luc.edu> <TANMOY.96Feb23175827@qcd.lanl.gov> <AD568E9F96689203B@mcdiala11.it.luc.edu>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <AD568E9F96689203B@mcdiala11.it.luc.edu>,
- Verne Arase <VArase@varase.it.luc.edu> wrote:
- >In C, a pointer to foo is practically the same as an array of foo. The only
- >difference (I believe) is that the array reserves storage, and can't have
- >its base address modified.
-
- No, they're really quite different.
-
- A pointer variable reserves storage - enough storage for an address. An
- array variable reserves enough storage for some constant number of objects.
- An array value cannot be modified, although members of the array may; by
- contrast, pointers can be modified, as can the things they point to.
-
- >If you have
-
- > foo *x; /* declare a pointer to foo */
- > foo y[10]; /* declare an array of foo */
-
- > x=y; /* set x to the address of y */
-
- >then x[n] and y[n] should have the same address and value.
-
- Yes, but the third comment is misleading. "x=y;" is setting x to the address
- of *y[0]*, not of y. (*'s for emphasis, not dereferencing.) The address of
- y is a thing of type pointer to array[10] of foo; the address of y[0], and
- the value you get when you refer to y in most expressions, is a thing
- of type pointer to foo. (There are exceptions, most notably sizeof. sizeof
- y will be 10 * sizeof(foo) - sizeof x will be sizeof(foo *).)
-
- >A pointer to foo
- >can be dereferenced as *x, and this would have a value which is a foo. A
- >reference such as x[n] is actually equivalent to *(x+n) in C (in C when you
- >add an integer 'n' to a pointer, it returns the address of the 'n'th
- >element of that pointed-to type).
-
- This is basically true; the [] operator (which is commutative) does
- exactly that. Just remember that it works on arrays because an array decays
- into a pointer in an expression context.
-
- (And no, there are *NO* C implementations in which [] is not commutative.
- There may be implementations of various languages in which it isn't, but they
- are not implementations of the C language.)
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-